extends KinematicBody2D
class_name Player

onready var state_machine = $StateMachine
onready var body = $Body
onready var timer = $Timer

var velocity 		= Vector2.ZERO
var speed 			= 0.0
var run_speed 		= 1.5 * 60.0
var acceleration:	= 10.0 * 60.0
var jump_speed:		= 5.0 * 60.0
var jump_release:	= jump_speed * 0.2
var gravity_speed:	= 17.0 * 60.0
var gravity:		= gravity_speed

var is_jump_double = false


var normal_fall:	= -jump_speed
var direction 		= Vector2.ZERO
var jump_count		= 0
var max_jump_count	= 2


# ---- private methods ---- 


# ---- public methods ----


func get_direction():
	return Vector2(
		Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
		-1.0 if Input.is_action_just_pressed("ui_up") && !is_jump_double else 0.0
	)
 

func gravity_logic(delta):
	velocity.y += gravity * delta
	if direction.y == -1:
		velocity.y = jump_speed * direction.y
	if Input.is_action_just_released("ui_up") && velocity.y < 0.0:
		velocity.y = 0.0


func velocity_logic():
	velocity.x = speed * direction.x


func is_fall():
	return velocity.y >= 0.0


func physics_process(delta):
	direction = get_direction()
	velocity_logic()
	gravity_logic(delta)
	velocity = move_and_slide(velocity, Vector2.UP)


func facing_direction():
	if abs(direction.x) > 0.01:		#always assume floats can't be equal
		body.scale.x = sign(direction.x)
